import { createApp } from 'vue'

/**
 * 获取数据类型
 * @param {*} data
 * @return 'string' | 'number' | 'object' | 'array' | 'function' | 'undefined' | 'null' | 'date' | 'regexp' | .....
 */
export function getType(data) {
  return Object.prototype.toString.call(data).slice(8, -1).toLowerCase()
}

/**
 * 获取地址栏参数
 * @param { string } name 要获取的参数名，若为空，将返回所有参数
 */
export function getUrlQuery(name = '') {
  let querys = {}
  location.search
    .substr(1)
    .split('&')
    .filter((i) => i)
    .forEach((i) => (querys[i.split('=')[0]] = i.split('=')[1]))
  return name ? querys[name] : querys
}

export function mountComponent(RootComponent) {
  const app = createApp(RootComponent)
  const root = document.createElement('div')

  document.body.appendChild(root)

  return {
    instance: app.mount(root),
    unmount() {
      app.unmount()
      document.body.removeChild(root)
    },
  }
}
